// 引入
var jwt = require('jsonwebtoken');
// 定义密钥
var secret = "djhdhshdsjfsjmdhwvgskjddefds"
function checkToken(req,res,next){
    let { token } = req.headers
    // 合法性
    jwt.verify(token, secret, (err, decoded) => {
        if (err) {
            console.log(err);
            res.send({
                code: 120,
                msg:"非法token,请登录"
            })
            return
        }
        console.log(decoded);
        let now = Date.now()
        let { limit, time } = decoded
        if (now - time >= limit) {
            res.send({
                code: 121,
                msg:'token过期，请重新登录'
            })
            return
        }
        // 放行
        next()
    })
}
// 抛出
module.exports = {
    checkToken
  }